home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / elispman.lha / elispman / elisp-15 (.txt) < prev    next >
GNU Info File  |  1993-06-01  |  46KB  |  845 lines

  1. This is Info file elisp, produced by Makeinfo-1.55 from the input file
  2. elisp.texi.
  3.    This is edition 2.0 of the GNU Emacs Lisp Reference Manual, for
  4. Emacs Version 19.
  5.    Published by the Free Software Foundation, 675 Massachusetts Avenue,
  6. Cambridge, MA 02139 USA
  7.    Copyright (C) 1990, 1991, 1992, 1993 Free Software Foundation, Inc.
  8.    Permission is granted to make and distribute verbatim copies of this
  9. manual provided the copyright notice and this permission notice are
  10. preserved on all copies.
  11.    Permission is granted to copy and distribute modified versions of
  12. this manual under the conditions for verbatim copying, provided that
  13. the entire resulting derived work is distributed under the terms of a
  14. permission notice identical to this one.
  15.    Permission is granted to copy and distribute translations of this
  16. manual into another language, under the above conditions for modified
  17. versions, except that this permission notice may be stated in a
  18. translation approved by the Foundation.
  19. File: elisp,  Node: Inheritance and Keymaps,  Next: Prefix Keys,  Prev: Creating Keymaps,  Up: Keymaps
  20. Inheritance and Keymaps
  21. =======================
  22.    A keymap can inherit the bindings of another keymap.  Do do this,
  23. make a keymap whose "tail" is another existing keymap to inherit from.
  24. Such a keymap looks like this:
  25.      (keymap BINDINGS... . OTHER-KEYMAP)
  26. The effect is that this keymap inherits all the bindings of
  27. OTHER-KEYMAP, whatever they may be at the time a key is looked up, but
  28. can add to them or override them with BINDINGS.
  29.    If you change the bindings in OTHER-KEYMAP using `define-key' or
  30. other key-binding functions, these changes are visible in the
  31. inheriting keymap unless shadowed by BINDINGS.  The converse is not
  32. true: if you use `define-key' to change the inheriting keymap, that
  33. affects BINDINGS, but has no effect on OTHER-KEYMAP.
  34.    Here is an example showing how to make a keymap that inherits from
  35. `text-mode-map':
  36.      (setq my-mode-map (cons 'keymap text-mode-map))
  37. File: elisp,  Node: Prefix Keys,  Next: Menu Keymaps,  Prev: Inheritance and Keymaps,  Up: Keymaps
  38. Prefix Keys
  39. ===========
  40.    A "prefix key" has an associated keymap which defines what to do
  41. with key sequences that start with the prefix key.  For example, `C-x'
  42. is a prefix key, and it uses a keymap which is also stored in the
  43. variable `ctl-x-map'.  Here is a list of the standard prefix keys of
  44. Emacs and their keymaps:
  45.    * `esc-map' is used for events that follow ESC.  Thus, the global
  46.      definitions of all meta characters are actually found here.  This
  47.      map is also the function definition of `ESC-prefix'.
  48.    * `help-map' is used for events that follow `C-h'.
  49.    * `mode-specific-map' is for events that follow `C-c'.  This map is
  50.      not actually mode specific; its name was chosen to be informative
  51.      for the user in `C-h b' (`display-bindings'), where it describes
  52.      the main use of the `C-c' prefix key.
  53.    * `ctl-x-map' is the variable name for the map used for events that
  54.      follow `C-x'.  This map is also the function definition of
  55.      `Control-X-prefix'.
  56.    * `ctl-x-4-map' is used for events that follow `C-x 4'.
  57.    * `ctl-x-5-map' used is for events that follow `C-x 5'.
  58.    * A nameless keymap is used for events that follow `C-x n'.  Others
  59.      are used for events following `C-x r' and `C-x a'.
  60.    The binding of a prefix key is the keymap to use for looking up the
  61. events that follow the prefix key.  (It may instead be a symbol whose
  62. function definition is a keymap.  The effect is the same, but the symbol
  63. serves as a name for the prefix key.)  Thus, the binding of `C-x' is
  64. the symbol `Control-X-prefix', whose function definition is the keymap
  65. for `C-x' commands.  (The same keymap is also the value of `ctl-x-map'.)
  66.    Prefix key definitions of this sort can appear in any active keymap.
  67. The definitions of `C-c', `C-x', `C-h' and ESC as prefix keys appear in
  68. the global map, so these prefix keys are always available.  Major and
  69. minor modes can redefine a key as a prefix by putting a prefix key
  70. definition for it in the local map or the minor mode's map.  *Note
  71. Active Keymaps::.
  72.    If a key is defined as a prefix in more than one active map, then the
  73. various definitions are in effect merged: the commands defined in the
  74. minor mode keymaps come first, followed by those in the local map's
  75. prefix definition, and then by those from the global map.
  76.    In the following example, we make `C-p' a prefix key in the local
  77. keymap, in such a way that `C-p' is identical to `C-x'.  Then the
  78. binding for `C-p C-f' is the function `find-file', just like `C-x C-f'.
  79. The key sequence `C-p 6' is not found in any active keymap.
  80.      (use-local-map (make-sparse-keymap))
  81.          => nil
  82.      (local-set-key "\C-p" ctl-x-map)
  83.          => nil
  84.      (key-binding "\C-p\C-f")
  85.          => find-file
  86.      
  87.      (key-binding "\C-p6")
  88.          => nil
  89.  - Function: define-prefix-command SYMBOL
  90.      This function defines SYMBOL as a prefix command: it creates a
  91.      full keymap and stores it as SYMBOL's function definition.
  92.      Storing the symbol as the binding of a key makes the key a prefix
  93.      key which has a name.  It also sets SYMBOL as a variable, to have
  94.      the keymap as its value.  The function returns SYMBOL.
  95.      In Emacs version 18, only the function definition of SYMBOL was
  96.      set, not the value as a variable.
  97. File: elisp,  Node: Menu Keymaps,  Next: Active Keymaps,  Prev: Prefix Keys,  Up: Keymaps
  98. Menu Keymaps
  99. ============
  100.    A keymap can define a menu as well as ordinary keys and mouse button
  101. meanings.  Menus are normally actuated with the mouse, but they can work
  102. with the keyboard also.
  103. * Menu:
  104. * Defining Menus::        How to make a keymap that defines a menu.
  105. * Mouse Menus::            How users actuate the menu with the mouse.
  106. * Keyboard Menus::        How they actuate it with the keyboard.
  107. * Menu Example::        Making a simple menu.
  108. * Menu Bar::            How to customize the menu bar.
  109. File: elisp,  Node: Defining Menus,  Next: Mouse Menus,  Up: Menu Keymaps
  110. Defining Menus
  111. --------------
  112.    A keymap is suitable for menu use if it has an "overall prompt
  113. string", which is a string that appears as an element of the keymap.
  114. (*Note Format of Keymaps::.)  The string should describe the purpose of
  115. the menu.  The easiest way to construct a keymap with a prompt string is
  116. to specify the string as an argument when you call `make-keymap' or
  117. `make-sparse-keymap' (*note Creating Keymaps::.).
  118.    The individual bindings in the menu keymap should also have prompt
  119. strings; these strings become the items displayed in the menu.  A
  120. binding with a prompt string looks like this:
  121.      (STRING . REAL-BINDING)
  122.    As far as `define-key' and `lookup-key' are concerned, the string is
  123. part of the event's binding.  However, only REAL-BINDING is used for
  124. executing the key.
  125.    You can also supply a second string, called the help string, as
  126. follows:
  127.      (STRING HELP-STRING . REAL-BINDING)
  128.    Currently Emacs does not actually use HELP-STRING; it knows only how
  129. to ignore HELP-STRING in order to extract REAL-BINDING.  In the future
  130. we hope to make HELP-STRING serve as extended documentation for the
  131. menu item, available on request.
  132.    The prompt string for a binding should be short--one or two words.
  133. It should describe the action of the command it corresponds to.
  134.    If REAL-BINDING is `nil', then STRING appears in the menu but cannot
  135. be selected.
  136.    If REAL-BINDING is a symbol, and has a non-`nil' `menu-enable'
  137. property, that property is an expression which controls whether the
  138. menu item is enabled.  Every time the keymap is used to display a menu,
  139. Emacs evaluates the expression, and it enables the menu item only if
  140. the expression's value is non-`nil'.  When a menu item is disabled, it
  141. is displayed in a "fuzzy" fashion, and cannot be selected with the
  142. mouse.
  143.    The order of items in the menu is the same as the order of bindings
  144. in the keymap.  Since `define-key' puts new bindings at the front, you
  145. should define the menu items starting at the bottom of the menu and
  146. moving to the top, if you care about the order.
  147. File: elisp,  Node: Mouse Menus,  Next: Keyboard Menus,  Prev: Defining Menus,  Up: Menu Keymaps
  148. Menus and the Mouse
  149. -------------------
  150.    The way to make a menu keymap produce a menu is to make it the
  151. definition of a prefix key.
  152.    When the prefix key ends with a mouse event, Emacs handles the menu
  153. keymap by popping up a visible menu, so that the user can select a
  154. choice with the mouse.  When the user clicks on a menu item, the event
  155. generated is whatever character or symbol has the binding which brought
  156. about that menu item.
  157.    It's often best to use a button-down event to trigger the menu.  Then
  158. the user can select a menu item by releasing the button.
  159.    A single keymap can appear as multiple menu panes, if you explicitly
  160. arrange for this.  The way to do this is to make a keymap for each pane,
  161. then create a binding for each of those maps in the main keymap of the
  162. menu.  Give each of these bindings a prompt string that starts with
  163. `@'.  The rest of the prompt string becomes the name of the pane.  See
  164. the file `lisp/mouse.el' for an example of this.  Any ordinary bindings
  165. with `@'-less prompt strings are grouped into one pane, which appears
  166. along with the other panes explicitly created for the submaps.
  167.    You can also get multiple panes from separate keymaps.  The full
  168. definition of a prefix key always comes from merging the definitions
  169. supplied by the various active keymaps (minor mode, local, and global).
  170. When more than one of these keymaps is a menu, each of them makes a
  171. separate pane or panes.  *Note Active Keymaps::.
  172.    A Lisp program can explicitly pop up a menu and receive the user's
  173. choice.  You can use keymaps for this also.  *Note Pop-Up Menus::.
  174. File: elisp,  Node: Keyboard Menus,  Next: Menu Example,  Prev: Mouse Menus,  Up: Menu Keymaps
  175. Menus and the Keyboard
  176. ----------------------
  177.    When a prefix key ending with a keyboard event (a character or
  178. function key) has a definition that is a menu keymap, the user can use
  179. the keyboard to choose a menu item.
  180.    Emacs displays the menu alternatives (the prompt strings of the
  181. bindings) in the echo area.  If they don't all fit at once, the user can
  182. type SPC to see the next line of alternatives.  Successive uses of SPC
  183. eventually get to the end of the menu and then cycle around to the
  184. beginning.
  185.    When the user has found the desired alternative from the menu, he or
  186. she should type the corresponding character--the one whose binding is
  187. that alternative.
  188.    In a menu intended for keyboard use, each menu item must clearly
  189. indicate what character to type.  The best convention to use is to make
  190. the character the first letter of the menu item prompt string.  That is
  191. something users will understand without being told.
  192.    This way of using menus in an Emacs-like editor was inspired by the
  193. Hierarkey system.
  194.  - Variable: menu-prompt-more-char
  195.      This variable specifies the character to use to ask to see the
  196.      next line of a menu.  Its initial value is 32, the code for SPC.
  197. File: elisp,  Node: Menu Example,  Next: Menu Bar,  Prev: Keyboard Menus,  Up: Menu Keymaps
  198. Menu Example
  199. ------------
  200.    Here is a simple example of how to set up a menu for mouse use.
  201.      (defvar my-menu-map
  202.        (make-sparse-keymap "Key Commands <==> Functions"))
  203.      (fset 'help-for-keys my-menu-map)
  204.      
  205.      (define-key my-menu-map [bindings]
  206.        '("List all keystroke commands" . describe-bindings))
  207.      (define-key my-menu-map [key]
  208.        '("Describe key briefly" . describe-key-briefly))
  209.      (define-key my-menu-map [key-verbose]
  210.        '("Describe key verbose" . describe-key))
  211.      (define-key my-menu-map [function]
  212.        '("Describe Lisp function" . describe-function))
  213.      (define-key my-menu-map [where-is]
  214.        '("Where is this command" . where-is))
  215.      
  216.      (define-key global-map [C-S-down-mouse-1] 'help-for-keys)
  217.    The symbols used in the key sequences bound in the menu are
  218. fictitious "function keys"; they don't appear on the keyboard, but that
  219. doesn't stop you from using them in the menu.  Their names were chosen
  220. to be mnemonic, because they show up in the output of `where-is' and
  221. `apropos' to identify the corresponding menu items.
  222.    However, if you want the menu to be usable from the keyboard as well,
  223. you must use real ASCII characters instead of fictitious function keys.
  224. File: elisp,  Node: Menu Bar,  Prev: Menu Example,  Up: Menu Keymaps
  225. The Menu Bar
  226. ------------
  227.    Under X Windows, each frame can have a "menu bar"--a permanently
  228. displayed menu stretching horizontally across the top of the frame.  The
  229. items of the menu bar are the subcommands of the fake "function key"
  230. `menu-bar', as defined by all the active keymaps.
  231.    To add an item to the menu bar, invent a fake "function key" of your
  232. own (let's call it KEY), and make a binding for the key sequence
  233. `[menu-bar KEY]'.  Most often, the binding is a menu keymap, so that
  234. pressing a button on the menu bar item leads to another menu.
  235.    When more than one active keymap defines the same fake function key
  236. for the menu bar, the item appears just once.  If the user clicks on
  237. that menu bar item, it brings up a single, combined submenu containing
  238. all the subcommands of that item--the global subcommands, the local
  239. subcommands, and the minor mode subcommands, all together.
  240.    In order for a frame to display a menu bar, its `menu-bar-lines'
  241. property must be greater than zero.  Emacs uses just one line for the
  242. menu bar itself; if you specify more than one line, the other lines
  243. serve to separate the menu bar from the windows in the frame.  We
  244. recommend you try one or two as the value of `menu-bar-lines'.  *Note X
  245. Frame Parameters::.
  246.    Here's an example of setting up a menu bar item:
  247.      (modify-frame-parameters (selected-frame) '((menu-bar-lines . 2)))
  248.      
  249.      ;; Make a menu keymap (with a prompt string)
  250.      ;; to be the menu bar item's definition.
  251.      (define-key global-map [menu-bar words]
  252.        (cons "Words" (make-sparse-keymap "Words")))
  253.      
  254.      ;; Make specific subcommands in the item's submenu.
  255.      (define-key global-map
  256.        [menu-bar words forward]
  257.        '("Forward word" . forward-word))
  258.      (define-key global-map
  259.        [menu-bar words backward]
  260.        '("Backward word" . backward-word))
  261. File: elisp,  Node: Active Keymaps,  Next: Key Lookup,  Prev: Menu Keymaps,  Up: Keymaps
  262. Active Keymaps
  263. ==============
  264.    Emacs normally contains many keymaps; at any given time, just a few
  265. of them are "active" in that they participate in the interpretation of
  266. user input.  These are the global keymap, the current buffer's local
  267. keymap, and the keymaps of any enabled minor modes.
  268.    The "global keymap" holds the bindings of keys that are defined
  269. regardless of the current buffer, such as `C-f'.  The variable
  270. `global-map' holds this keymap, which is always active.
  271.    Each buffer may have another keymap, its "local keymap", which may
  272. contain new or overriding definitions for keys.  At all times, the
  273. current buffer's local keymap is active.  Text properties can specify an
  274. alternative local map for certain parts of the buffer; see *Note
  275. Special Properties::.
  276.    Each minor mode may have a keymap; if it does, the keymap is active
  277. whenever the minor mode is enabled.
  278.    All the active keymaps are used together to determine what command to
  279. execute when a key is entered.  The key lookup proceeds as described
  280. earlier (*note Key Lookup::.), but Emacs *first* searches for the key
  281. in the minor mode maps (one map at a time); if they do not supply a
  282. binding for the key, Emacs searches the local map; if that too has no
  283. binding, Emacs then searches the global map.
  284.    Since every buffer that uses the same major mode normally uses the
  285. very same local keymap, it may appear as if the keymap is local to the
  286. mode.  A change to the local keymap of a buffer (using `local-set-key',
  287. for example) will be seen also in the other buffers that share that
  288. keymap.
  289.    The local keymaps that are used for Lisp mode, C mode, and several
  290. other major modes exist even if they have not yet been used.  These
  291. local maps are the values of the variables `lisp-mode-map',
  292. `c-mode-map', and so on.  For most other modes, which are less
  293. frequently used, the local keymap is constructed only when the mode is
  294. used for the first time in a session.
  295.    The minibuffer has local keymaps, too; they contain various
  296. completion and exit commands.  *Note Minibuffers::.
  297.    *Note Standard Keymaps::, for a list of standard keymaps.
  298.  - Variable: global-map
  299.      This variable contains the default global keymap that maps Emacs
  300.      keyboard input to commands.  Normally this keymap is the global
  301.      keymap.  The default global keymap is a full keymap that binds
  302.      `self-insert-command' to all of the printing characters.
  303.  - Function: current-global-map
  304.      This function returns the current global keymap.  This is always
  305.      the same as the value of `global-map' unless you change one or the
  306.      other.
  307.           (current-global-map)
  308.           => (keymap [set-mark-command beginning-of-line ...
  309.                       delete-backward-char])
  310.  - Function: current-local-map
  311.      This function returns the current buffer's local keymap, or `nil'
  312.      if it has none.  In the following example, the keymap for the
  313.      `*scratch*' buffer (using Lisp Interaction mode) is a sparse keymap
  314.      in which the entry for ESC, ASCII code 27, is another sparse
  315.      keymap.
  316.           (current-local-map)
  317.           => (keymap
  318.               (10 . eval-print-last-sexp)
  319.               (9 . lisp-indent-line)
  320.               (127 . backward-delete-char-untabify)
  321.               (27 keymap
  322.                   (24 . eval-defun)
  323.                   (17 . indent-sexp)))
  324.  - Function: current-minor-mode-maps
  325.      This function returns a list of the keymaps of currently enabled
  326.      minor modes.
  327.  - Function: use-global-map KEYMAP
  328.      This function makes KEYMAP the new current global keymap.  It
  329.      returns `nil'.
  330.      It is very unusual to change the global keymap.
  331.  - Function: use-local-map KEYMAP
  332.      This function makes KEYMAP the new current local keymap of the
  333.      current buffer.  If KEYMAP is `nil', then there will be no local
  334.      keymap.  It returns `nil'.  Most major modes use this function.
  335.  - Variable: minor-mode-map-alist
  336.      This variable is an alist describing keymaps that may or may not be
  337.      active according to the values of certain variables.  Its elements
  338.      look like this:
  339.           (VARIABLE . KEYMAP)
  340.      The keymap KEYMAP is active whenever VARIABLE has a non-`nil'
  341.      value.  Typically VARIABLE is the variable which enables or
  342.      disables a minor mode.  *Note Keymaps and Minor Modes::.
  343.      When more than one minor mode keymap is active, their order of
  344.      priority is the order of `minor-mode-map-alist'.
  345.      See also `minor-mode-key-binding' in *Note Functions for Key
  346.      Lookup::.
  347. File: elisp,  Node: Key Lookup,  Next: Functions for Key Lookup,  Prev: Active Keymaps,  Up: Keymaps
  348. Key Lookup
  349. ==========
  350.    "Key lookup" is the process of finding the binding of a key sequence
  351. from a given keymap.  Actual execution of the binding is not part of
  352. key lookup.
  353.    Key lookup uses just the event types of each event in the key
  354. sequence; the rest of the event is ignored.  In fact, a key sequence
  355. used for key lookup may designate mouse events with just their types
  356. (symbols) instead of with entire mouse events (lists).  *Note Input
  357. Events::.  Such a pseudo-key-sequence is insufficient for
  358. `command-execute', but it is sufficient for looking up or rebinding a
  359.    When the key sequence consists of multiple events, key lookup
  360. processes the events sequentially: the binding of the first event is
  361. found, and must be a keymap; then the second event's binding is found in
  362. that keymap, and so on until all the events in the key sequence are used
  363. up.  (The binding thus found for the last event may or may not be a
  364. keymap.)  Thus, the process of key lookup is defined in terms of a
  365. simpler process for looking up a single event in a keymap.  How that is
  366. done depends on the type of object associated with the event in that
  367. keymap.
  368.    Let's use the term "keymap entry" to describe the value directly
  369. associated with an event type in a keymap.  While any Lisp object may be
  370. stored as a keymap entry, not all make sense for key lookup.  Here is a
  371. list of the meaningful kinds of keymap entries:
  372. `nil'
  373.      `nil' means that the events used so far in the lookup form an
  374.      undefined key.  When a keymap fails to mention an event type at
  375.      all, that is equivalent to an entry of `nil' for that type.
  376. KEYMAP
  377.      The events used so far in the lookup form a prefix key.  The next
  378.      event of the key sequence is looked up in KEYMAP.
  379. COMMAND
  380.      The events used so far in the lookup form a complete key, and
  381.      COMMAND is its binding.
  382. STRING
  383. VECTOR
  384.      The events used so far in the lookup form a complete key, whose
  385.      binding is a keyboard macro.  See *Note Keyboard Macros::, for more
  386.      information.
  387.      The meaning of a list depends on the types of the elements of the
  388.      list.
  389.         * If the CAR of LIST is the symbol `keymap', then the list is a
  390.           keymap, and is treated as a keymap (see above).
  391.         * If the CAR of LIST is `lambda', then the list is a lambda
  392.           expression.  This is presumed to be a command, and is treated
  393.           as such (see above).
  394.         * If the CAR of LIST is a keymap and the CDR is an event type,
  395.           then this is an "indirect entry":
  396.                (OTHERMAP . OTHERTYPE)
  397.           When key lookup encounters an indirect entry, it looks up
  398.           instead the binding of OTHERTYPE in OTHERMAP and uses that.
  399.           This feature permits you to define one key as an alias for
  400.           another key.  For example, an entry whose CAR is the keymap
  401.           called `esc-map' and whose CDR is 32 (the code for space)
  402.           means, "Use the global binding of `Meta-SPC', whatever that
  403.           may be."
  404.         * If the CAR of LIST is a string, it serves as a menu item name
  405.           if the keymap is used as a menu.  For executing the key, the
  406.           string is discarded and the CDR of LIST is used instead.
  407.           (Any number of strings can be discarded from the front of the
  408.           list in this way.) *Note Menu Keymaps::.
  409. SYMBOL
  410.      The function definition of SYMBOL is used in place of SYMBOL.  If
  411.      that too is a symbol, then this process is repeated, any number of
  412.      times.  Ultimately this should lead to an object which is a
  413.      keymap, a command or a keyboard macro.  A list is allowed if it is
  414.      a keymap or a command, but indirect entries are not understood
  415.      when found via symbols.
  416.      Note that keymaps and keyboard macros (strings and vectors) are not
  417.      valid functions, so a symbol with a keymap, string or vector as its
  418.      function definition is also invalid as a function.  It is, however,
  419.      valid as a key binding.  If the definition is a keyboard macro,
  420.      then the symbol is also valid as an argument to `command-execute'
  421.      (*note Interactive Call::.).
  422.      The symbol `undefined' is worth special mention: it means to treat
  423.      the key as undefined.  Strictly speaking, the key is defined, and
  424.      its binding is the command `undefined'; but that command does the
  425.      same thing that is done automatically for an undefined key: it
  426.      rings the bell (by calling `ding') but does not signal an error.
  427.      `undefined' is used in local keymaps to override a global key
  428.      binding and make the key "undefined" locally.  A local binding of
  429.      `nil' would fail to do this because it would not override the
  430.      global binding.
  431. ANYTHING ELSE
  432.      If any other type of object is found, the events used so far in the
  433.      lookup form a complete key, and the object is its binding, but the
  434.      binding is not executable as a command.
  435.    In short, a keymap entry may be a keymap, a command, a keyboard
  436. macro, a symbol which leads to one of them, or an indirection or `nil'.
  437. Here is an example of a sparse keymap with two characters bound to
  438. commands and one bound to another keymap.  This map is the normal value
  439. of `emacs-lisp-mode-map'.  Note that 9 is the code for TAB, 127 for
  440. DEL, 27 for ESC, 17 for `C-q' and 24 for `C-x'.
  441.      (keymap (9 . lisp-indent-line)
  442.              (127 . backward-delete-char-untabify)
  443.              (27 keymap (17 . indent-sexp) (24 . eval-defun)))
  444. File: elisp,  Node: Functions for Key Lookup,  Next: Changing Key Bindings,  Prev: Key Lookup,  Up: Keymaps
  445. Functions for Key Lookup
  446. ========================
  447.    Here are the functions and variables pertaining to key lookup.
  448.  - Function: lookup-key KEYMAP KEY &optional ACCEPT-DEFAULTS
  449.      This function returns the definition of KEY in KEYMAP.  If the
  450.      string or vector KEY is not a valid key sequence according to the
  451.      prefix keys specified in KEYMAP (which means it is "too long" and
  452.      has extra events at the end), then the value is a number, the
  453.      number of events at the front of KEY that compose a complete key.
  454.      If ACCEPT-DEFAULTS is non-`nil', then `lookup-key' considers
  455.      default bindings as well as bindings for the specific events in
  456.      KEY.  Otherwise, `lookup-key' reports only bindings for the
  457.      specific sequence KEY, ignoring default bindings except when an
  458.      element of KEY is `t'.
  459.      All the other functions described in this chapter that look up
  460.      keys use `lookup-key'.
  461.           (lookup-key (current-global-map) "\C-x\C-f")
  462.               => find-file
  463.           (lookup-key (current-global-map) "\C-x\C-f12345")
  464.               => 2
  465.      If KEY contains a meta character, that character is implicitly
  466.      replaced by a two-character sequence: the value of
  467.      `meta-prefix-char', followed by the corresponding non-meta
  468.      character.  Thus, the first example below is handled by conversion
  469.      into the second example.
  470.           (lookup-key (current-global-map) "\M-f")
  471.               => forward-word
  472.           (lookup-key (current-global-map) "\ef")
  473.               => forward-word
  474.      This function does not modify the specified events in ways that
  475.      discard information as `read-key-sequence' does (*note Key
  476.      Sequence Input::.).  In particular, it does not convert letters to
  477.      lower case and it does not change drag events to clicks.
  478.  - Command: undefined
  479.      Used in keymaps to undefine keys.  It calls `ding', but does not
  480.      cause an error.
  481.  - Function: key-binding KEY &optional ACCEPT-DEFAULTS
  482.      This function returns the binding for KEY in the current keymaps,
  483.      trying all the active keymaps.  The result is `nil' if KEY is
  484.      undefined in the keymaps.
  485.      The argument ACCEPT-DEFAULTS controls checking for default
  486.      bindings, as in `lookup-key'.
  487.      An error is signaled if KEY is not a string or a vector.
  488.           (key-binding "\C-x\C-f")
  489.               => find-file
  490.  - Function: local-key-binding KEY &optional ACCEPT-DEFAULTS
  491.      This function returns the binding for KEY in the current local
  492.      keymap, or `nil' if it is undefined there.
  493.      The argument ACCEPT-DEFAULTS controls checking for default
  494.      bindings, as in `lookup-key' (above).
  495.  - Function: global-key-binding KEY &optional ACCEPT-DEFAULTS
  496.      This function returns the binding for command KEY in the current
  497.      global keymap, or `nil' if it is undefined there.
  498.      The argument ACCEPT-DEFAULTS controls checking for default
  499.      bindings, as in `lookup-key' (above).
  500.  - Function: minor-mode-key-binding KEY &optional ACCEPT-DEFAULTS
  501.      This function returns a list of all the active minor mode bindings
  502.      of KEY.  More precisely, it returns an alist of pairs `(MODENAME .
  503.      BINDING)', where MODENAME is the the variable which enables the
  504.      minor mode, and BINDING is KEY's binding in that mode.  If KEY has
  505.      no minor-mode bindings, the value is `nil'.
  506.      If the first binding is a non-prefix, all subsequent bindings from
  507.      other minor modes are omitted, since they would be completely
  508.      shadowed.  Similarly, the list omits non-prefix bindings that
  509.      follow prefix bindings.
  510.      The argument ACCEPT-DEFAULTS controls checking for default
  511.      bindings, as in `lookup-key' (above).
  512.  - Variable: meta-prefix-char
  513.      This variable is the meta-prefix character code.  It is used when
  514.      translating a meta character to a two-character sequence so it can
  515.      be looked up in a keymap.  For useful results, the value should be
  516.      a prefix event (*note Prefix Keys::.).  The default value is 27,
  517.      which is the ASCII code for ESC.
  518.      As long as the value of `meta-prefix-char' remains 27, key lookup
  519.      translates `M-b' into `ESC b', which is normally defined as the
  520.      `backward-word' command.  However, if you set `meta-prefix-char'
  521.      to 24, the code for `C-x', then Emacs will translate `M-b' into
  522.      `C-x b', whose standard binding is the `switch-to-buffer' command.
  523.           meta-prefix-char                    ; The default value.
  524.                => 27
  525.           (key-binding "\M-b")
  526.                => backward-word
  527.           ?\C-x                               ; The print representation
  528.                => 24                          ;   of a character.
  529.           (setq meta-prefix-char 24)
  530.                => 24
  531.           (key-binding "\M-b")
  532.                => switch-to-buffer            ; Now, typing `M-b' is
  533.                                               ;   like typing `C-x b'.
  534.           
  535.           (setq meta-prefix-char 27)          ; Avoid confusion!
  536.                => 27                          ; Restore the default value!
  537. File: elisp,  Node: Changing Key Bindings,  Next: Key Binding Commands,  Prev: Functions for Key Lookup,  Up: Keymaps
  538. Changing Key Bindings
  539. =====================
  540.    The way to rebind a key is to change its entry in a keymap.  You can
  541. change the global keymap, so that the change is effective in all buffers
  542. (except those that override the global binding with a local one).  Or
  543. you can change the current buffer's local map, which usually affects all
  544. buffers using the same major mode.  The `global-set-key' and
  545. `local-set-key' functions are convenient interfaces for these
  546. operations.  Or you can use `define-key' and specify explicitly which
  547. map to change.
  548.    People often use `global-set-key' in their `.emacs' file for simple
  549. customization.  For example,
  550.      (global-set-key "\C-x\C-\\" 'next-line)
  551.      (global-set-key [?\C-x ?\C-\\] 'next-line)
  552. redefines `C-x C-\' to move down a line.
  553.      (global-set-key [M-mouse-1] 'mouse-set-point)
  554. redefines the first (leftmost) mouse button, typed with the Meta key, to
  555. set point where you click.
  556.    In writing the key sequence to rebind, it is useful to use the
  557. special escape sequences for control and meta characters (*note String
  558. Type::.).  The syntax `\C-' means that the following character is a
  559. control character and `\M-' means that the following character is a meta
  560. character.  Thus, the string `"\M-x"' is read as containing a single
  561. `M-x', `"\C-f"' is read as containing a single `C-f', and `"\M-\C-x"'
  562. and `"\C-\M-x"' are both read as containing a single `C-M-x'.
  563.    For the functions below, an error is signaled if KEYMAP is not a
  564. keymap or if KEY is not a string or vector representing a key sequence.
  565. However, you can use event types (symbols) as shorthand for events
  566. that are lists.
  567.  - Function: define-key KEYMAP KEY BINDING
  568.      This function sets the binding for KEY in KEYMAP.  (If KEY is more
  569.      than one event long, the change is actually made in another keymap
  570.      reached from KEYMAP.)  The argument BINDING can be any Lisp
  571.      object, but only certain types are meaningful.  (For a list of
  572.      meaningful types, see *Note Key Lookup::.) The value returned by
  573.      `define-key' is BINDING.
  574.      Every prefix of KEY must be a prefix key (i.e., bound to a keymap)
  575.      or undefined; otherwise an error is signaled.
  576.      If some prefix of KEY is undefined, then `define-key' defines it
  577.      as a prefix key so that the rest of KEY may be defined as
  578.      specified.
  579.      The following example creates a sparse keymap and makes a number of
  580.      bindings:
  581.           (setq map (make-sparse-keymap))
  582.               => (keymap)
  583.           (define-key map "\C-f" 'forward-char)
  584.               => forward-char
  585.           map
  586.               => (keymap (6 . forward-char))
  587.           
  588.           ;; Build sparse submap for `C-x' and bind `f' in that.
  589.           (define-key map "\C-xf" 'forward-word)
  590.               => forward-word
  591.           map
  592.           => (keymap
  593.               (24 keymap                ; `C-x'
  594.                   (102 . forward-word)) ;      `f'
  595.               (6 . forward-char))       ; `C-f'
  596.           
  597.           ;; Bind `C-p' to the `ctl-x-map'.
  598.           (define-key map "\C-p" ctl-x-map)
  599.           ;; `ctl-x-map'
  600.           => [nil ... find-file ... backward-kill-sentence]
  601.           
  602.           ;; Bind `C-f' to `foo' in the `ctl-x-map'.
  603.           (define-key map "\C-p\C-f" 'foo)
  604.           => 'foo
  605.           map
  606.           => (keymap     ; Note `foo' in `ctl-x-map'.
  607.               (16 keymap [nil ... foo ... backward-kill-sentence])
  608.               (24 keymap
  609.                   (102 . forward-word))
  610.               (6 . forward-char))
  611.      Note that storing a new binding for `C-p C-f' actually works by
  612.      changing an entry in `ctl-x-map', and this has the effect of
  613.      changing the bindings of both `C-p C-f' and `C-x C-f' in the
  614.      default global map.
  615.  - Function: substitute-key-definition OLDDEF NEWDEF KEYMAP &optional
  616.           OLDMAP
  617.      This function replaces OLDDEF with NEWDEF for any keys in KEYMAP
  618.      that were bound to OLDDEF.  In other words, OLDDEF is replaced
  619.      with NEWDEF wherever it appears.  The function returns `nil'.
  620.      For example, this redefines `C-x C-f', if you do it in an Emacs
  621.      with standard bindings:
  622.           (substitute-key-definition
  623.            'find-file 'find-file-read-only (current-global-map))
  624.      If OLDMAP is non-`nil', then its bindings determine which keys to
  625.      rebind.  The rebindings still happen in NEWMAP, not in OLDMAP.
  626.      Thus, you can change one map under the control of the bindings in
  627.      another.  For example,
  628.           (substitute-key-definition
  629.             'delete-backward-char 'my-funny-delete
  630.             my-map global-map)
  631.      puts the special deletion command in `my-map' for whichever keys
  632.      are globally bound to the standard deletion command.
  633.      Here is an example showing a keymap before and after substitution:
  634.           (setq map '(keymap
  635.                       (?1 . olddef-1)
  636.                       (?2 . olddef-2)
  637.                       (?3 . olddef-1)))
  638.           => (keymap (49 . olddef-1) (50 . olddef-2) (51 . olddef-1))
  639.           (substitute-key-definition 'olddef-1 'newdef map)
  640.           => nil
  641.           map
  642.           => (keymap (49 . newdef) (50 . olddef-2) (51 . newdef))
  643.  - Function: suppress-keymap KEYMAP &optional NODIGITS
  644.      This function changes the contents of the full keymap KEYMAP by
  645.      replacing the self-insertion commands for numbers with the
  646.      `digit-argument' function, unless NODIGITS is non-`nil', and by
  647.      replacing the functions for the rest of the printing characters
  648.      with `undefined'.  This means that ordinary insertion of text is
  649.      impossible in a buffer with a local keymap on which
  650.      `suppress-keymap' has been called.
  651.      `suppress-keymap' returns `nil'.
  652.      The `suppress-keymap' function does not make it impossible to
  653.      modify a buffer, as it does not suppress commands such as `yank'
  654.      and `quoted-insert'.  To prevent any modification of a buffer, make
  655.      it read-only (*note Read Only Buffers::.).
  656.      Since this function modifies KEYMAP, you would normally use it on
  657.      a newly created keymap.  Operating on an existing keymap that is
  658.      used for some other purpose is likely to cause trouble; for
  659.      example, suppressing `global-map' would make it impossible to use
  660.      most of Emacs.
  661.      Most often, `suppress-keymap' is used to initialize local keymaps
  662.      of modes such as Rmail and Dired where insertion of text is not
  663.      desirable and the buffer is read-only.  Here is an example taken
  664.      from the file `emacs/lisp/dired.el', showing how the local keymap
  665.      for Dired mode is set up:
  666.           ...
  667.             (setq dired-mode-map (make-keymap))
  668.             (suppress-keymap dired-mode-map)
  669.             (define-key dired-mode-map "r" 'dired-rename-file)
  670.             (define-key dired-mode-map "\C-d" 'dired-flag-file-deleted)
  671.             (define-key dired-mode-map "d" 'dired-flag-file-deleted)
  672.             (define-key dired-mode-map "v" 'dired-view-file)
  673.             (define-key dired-mode-map "e" 'dired-find-file)
  674.             (define-key dired-mode-map "f" 'dired-find-file)
  675.             ...
  676. File: elisp,  Node: Key Binding Commands,  Next: Scanning Keymaps,  Prev: Changing Key Bindings,  Up: Keymaps
  677. Commands for Binding Keys
  678. =========================
  679.    This section describes some convenient interactive interfaces for
  680. changing key bindings.  They work by calling `define-key'.
  681.  - Command: global-set-key KEY DEFINITION
  682.      This function sets the binding of KEY in the current global map to
  683.      DEFINITION.
  684.           (global-set-key KEY DEFINITION)
  685.           ==
  686.           (define-key (current-global-map) KEY DEFINITION)
  687.  - Command: global-unset-key KEY
  688.      This function removes the binding of KEY from the current global
  689.      map.
  690.      One use of this function is in preparation for defining a longer
  691.      key which uses it implicitly as a prefix--which would not be
  692.      allowed if KEY has a non-prefix binding.  For example:
  693.           (global-unset-key "\C-l")
  694.               => nil
  695.           (global-set-key "\C-l\C-l" 'redraw-display)
  696.               => nil
  697.      This function is implemented simply using `define-key':
  698.           (global-unset-key KEY)
  699.           ==
  700.           (define-key (current-global-map) KEY nil)
  701.  - Command: local-set-key KEY DEFINITION
  702.      This function sets the binding of KEY in the current local keymap
  703.      to DEFINITION.
  704.           (local-set-key KEY DEFINITION)
  705.           ==
  706.           (define-key (current-local-map) KEY DEFINITION)
  707.  - Command: local-unset-key KEY
  708.      This function removes the binding of KEY from the current local
  709.      map.
  710.           (local-unset-key KEY)
  711.           ==
  712.           (define-key (current-local-map) KEY nil)
  713. File: elisp,  Node: Scanning Keymaps,  Prev: Key Binding Commands,  Up: Keymaps
  714. Scanning Keymaps
  715. ================
  716.    This section describes functions used to scan all the current keymaps
  717. for the sake of printing help information.
  718.  - Function: accessible-keymaps KEYMAP
  719.      This function returns a list of all the keymaps that can be
  720.      accessed (via prefix keys) from KEYMAP.  The value is an
  721.      association list with elements of the form `(KEY . MAP)', where
  722.      KEY is a prefix key whose definition in KEYMAP is MAP.
  723.      The elements of the alist are ordered so that the KEY increases in
  724.      length.  The first element is always `("" . KEYMAP)', because the
  725.      specified keymap is accessible from itself with a prefix of no
  726.      events.
  727.      In the example below, the returned alist indicates that the key
  728.      ESC, which is displayed as `^[', is a prefix key whose definition
  729.      is the sparse keymap `(keymap (83 . center-paragraph) (115 .
  730.      foo))'.
  731.           (accessible-keymaps (current-local-map))
  732.           =>(("" keymap
  733.                 (27 keymap   ; Note this keymap for ESC is repeated below.
  734.                     (83 . center-paragraph)
  735.                     (115 . center-line))
  736.                 (9 . tab-to-tab-stop))
  737.           ("^[" keymap
  738.               (83 . center-paragraph)
  739.               (115 . foo)))
  740.      In the following example, `C-h' is a prefix key that uses a sparse
  741.      keymap starting `(keymap (118 . describe-variable)...)'.  Another
  742.      prefix, `C-x 4', uses a keymap which happens to be `ctl-x-4-map'.
  743.      The event `mode-line' is one of several dummy events used as
  744.      prefixes for mouse actions in special parts of a window.
  745.           (accessible-keymaps (current-global-map))
  746.           => (("" keymap [set-mark-command beginning-of-line ...
  747.                              delete-backward-char])
  748.           ("^H" keymap (118 . describe-variable) ...
  749.                (8 . help-for-help))
  750.               ("^X" keymap [x-flush-mouse-queue ... backward-kill-sentence])
  751.               ("^[" keymap [mark-sexp backward-sexp ... backward-kill-word])
  752.               ("^X4" keymap (15 . display-buffer) ...)
  753.               ([mode-line] keymap
  754.                (S-mouse-2 . mouse-split-window-horizontally) ...))
  755.      These are not all the keymaps you would see in an actual case.
  756.  - Function: where-is-internal COMMAND &optional KEYMAP FIRSTONLY
  757.      This function returns a list of key sequences (of any length) that
  758.      are bound to COMMAND in KEYMAP and the global keymap.  The
  759.      argument COMMAND can be any object; it is compared with all keymap
  760.      entries using `eq'.  If KEYMAP is not supplied, then the global
  761.      map alone is used.
  762.      If FIRSTONLY is non-`nil', then the value is a single string
  763.      representing the first key sequence found, rather than a list of
  764.      all possible key sequences.
  765.      This function is used by `where-is' (*note Help: (emacs)Help.).
  766.           (where-is-internal 'describe-function)
  767.               => ("\^hf" "\^hd")
  768.  - Command: describe-bindings
  769.      This function creates a listing of all defined keys, and their
  770.      definitions.  The listing is put in a buffer named `*Help*', which
  771.      is then displayed in a window.
  772.      A meta character is shown as ESC followed by the corresponding
  773.      non-meta character.  Control characters are indicated with `C-'.
  774.      When several characters with consecutive ASCII codes have the same
  775.      definition, they are shown together, as `FIRSTCHAR..LASTCHAR'.  In
  776.      this instance, you need to know the ASCII codes to understand
  777.      which characters this means.  For example, in the default global
  778.      map, the characters `SPC .. ~' are described by a single line.
  779.      SPC is ASCII 32, `~' is ASCII 126, and the characters between them
  780.      include all the normal printing characters, (e.g., letters,
  781.      digits, punctuation, etc.); all these characters are bound to
  782.      `self-insert-command'.
  783. File: elisp,  Node: Modes,  Next: Documentation,  Prev: Keymaps,  Up: Top
  784. Major and Minor Modes
  785. *********************
  786.    A "mode" is a set of definitions that customize Emacs and can be
  787. turned on and off while you edit.  There are two varieties of modes:
  788. "major modes", which are mutually exclusive and used for editing
  789. particular kinds of text, and "minor modes", which provide features that
  790. may be enabled individually.
  791.    This chapter covers both major and minor modes, the way they are
  792. indicated in the mode line, and how they run hooks supplied by the user.
  793. Related topics such as keymaps and syntax tables are covered in separate
  794. chapters.  (*Note Keymaps::, and *Note Syntax Tables::.)
  795. * Menu:
  796. * Major Modes::        Defining major modes.
  797. * Minor Modes::        Defining minor modes.
  798. * Mode Line Format::   Customizing the text that appears in the mode line.
  799. * Hooks::              How to use hooks; how to write code that provides hooks.
  800. File: elisp,  Node: Major Modes,  Next: Minor Modes,  Prev: Modes,  Up: Modes
  801. Major Modes
  802. ===========
  803.    Major modes specialize Emacs for editing particular kinds of text.
  804. Each buffer has only one major mode at a time.
  805.    The least specialized major mode is called "Fundamental mode".  This
  806. mode has no mode-specific definitions or variable settings, so each
  807. Emacs command behaves in its default manner, and each option is in its
  808. default state.  All other major modes redefine various keys and options.
  809. For example, Lisp Interaction mode provides special key bindings for
  810. LFD (`eval-print-last-sexp'), TAB (`lisp-indent-line'), and other keys.
  811.    When you need to write several editing commands to help you perform a
  812. specialized editing task, creating a new major mode is usually a good
  813. idea.  In practice, writing a major mode is easy (in contrast to
  814. writing a minor mode, which is often difficult).
  815.    If the new mode is similar to an old one, it is often unwise to
  816. modify the old one to serve two purposes, since it may become harder to
  817. use and maintain.  Instead, copy and rename an existing major mode
  818. definition and alter it for its new function.  For example, Rmail Edit
  819. mode, which is in `emacs/lisp/rmailedit.el', is a major mode that is
  820. very similar to Text mode except that it provides three additional
  821. commands.  Its definition is distinct from that of Text mode, but was
  822. derived from it.
  823.    Rmail Edit mode is an example of a case where one piece of text is
  824. put temporarily into a different major mode so it can be edited in a
  825. different way (with ordinary Emacs commands rather than Rmail).  In such
  826. cases, the temporary major mode usually has a command to switch back to
  827. the buffer's usual mode (Rmail mode, in this case).  You might be
  828. tempted to present the temporary redefinitions inside a recursive edit
  829. and restore the usual ones when the user exits; but this is a bad idea
  830. because it constrains the user's options when it is done in more than
  831. one buffer: recursive edits must be exited most-recently-entered first.
  832. Using alternative major modes avoids this limitation.  *Note Recursive
  833. Editing::.
  834.    The standard GNU Emacs Lisp library directory contains the code for
  835. several major modes, in files including `text-mode.el', `texinfo.el',
  836. `lisp-mode.el', `c-mode.el', and `rmail.el'.  You can look at these
  837. libraries to see how modes are written.  Text mode is perhaps the
  838. simplest major mode aside from Fundamental mode.  Rmail mode is a
  839. rather complicated, full-featured mode.
  840. * Menu:
  841. * Major Mode Conventions::  Coding conventions for keymaps, etc.
  842. * Example Major Modes::     Text mode and Lisp modes.
  843. * Auto Major Mode::         How Emacs chooses the major mode automatically.
  844. * Mode Help::               Finding out how to use a mode.
  845.